package com.example.finaltool.model;

import com.example.finaltool.model.db.info.Range;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @author dengbw
 * @date 2021/8/16 1:21 下午
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RangeCheck {
    private Long consultId;
    private String infoType;
    private Long subTenantId;

    public RangeCheck(Range r) {
        consultId = r.getConsultId();
        infoType = r.getInfoType();
        subTenantId = r.getSubTenantId();
    }

    public RangeCheck(RangeQuery r) {
        consultId = r.getConsultId();
        infoType = r.getInfoType();
        subTenantId = r.getSubTenantId();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof RangeCheck)) {
            return false;
        }
        RangeCheck c = (RangeCheck) obj;
        return checkConsultId(c.consultId) && checkInfoType(c.infoType) && checkSubTenantId(c.subTenantId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(consultId, infoType, subTenantId);
    }

    private boolean checkConsultId(Long id) {
        if (consultId == null) {
            return id == null;
        } else {
            return consultId.equals(id);
        }
    }

    private boolean checkInfoType(String id) {
        if (StringUtils.isBlank(infoType)) {
            return StringUtils.isBlank(id);
        } else {
            return infoType.equals(id);
        }
    }

    private boolean checkSubTenantId(Long id) {
        if (subTenantId == null) {
            return id == null;
        } else {
            return subTenantId.equals(id);
        }
    }
}
